function tableshua (){
    tableIn.reload("#ptable");
}
//文档加载完成后
$(function (){

    ajaxtree();
    ajaxtable();
    resizeRem();

})

var tableIn = null;

function resizeRem() {
    if (window.innerWidth < window.innerHeight)
    {
        var html = document.querySelector('html');
        var rem = html.offsetHeight / 10.80;
        html.style.fontSize = rem + "px";
    }
    else {
        var html = document.querySelector('html');
        var rem = html.offsetWidth / 19.20;
        html.style.fontSize = rem + "px";
    }
}

function ajaxtree(){
    //ajax 请求数据
    $.ajax({
        type:'get',
        url:'/Cate/advanced/getpid',
        data:{},
        dataType:'json',
        success:function (res){
            treerender(res);
        }
    })

}

function aa(res1,res2) {
    if(res1==0){
        return '<div style="line-height: 50px">'+'未审核' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }else if(res1==1){
        return '<div style="line-height: 50px">'+'审核中' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }else if(res1==2){
        return '<div style="line-height: 50px">'+'已审核' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }
}

function auditprocess(id){
    localStorage.setItem("productId",id);
    layer.open({
        type: 2,
        title: '审核详情',
        content: "productsh.html",
        area: ['80%', '80%'],
    })
}


function treerender(data){
    layui.use(['tree','table','layer','form'],function (){
        var tree = layui.tree,
            layer= layui.layer,
            form = layui.form,
            table= layui.table;

        tree.render({
            elem:'#ptree',
            data:data,
            id:'ptreeID',
            edit:true,//修改删除
            click:function (obj){
                console.log(obj.data);//得到当前点击的节点数据  id title , children
                // 将节点的id  存入 表单
                $("input[name='parentId']").val(obj.data.id);
                layer.open({
                    type:1,
                    title:'添加子节点',
                    content:$('#addhtml'),
                    area:['50%','50%'],
                    end:function (){
                        //弹框被销毁后，不管确定还是取消，都会调用
                        $('#addhtml').css('display','none');
                        tree.reload('ptreeID');
                    }
                })
            },
            operate:function (obj) {
                var type = obj.type;//得到操作类型：update，del
                var data = obj.data;//得到当前节点的数据

                console.log(data);
                //debugger
                if (type == 'update') {
                    //debugger
                    //修改
                    //ajax请求
                    $.ajax({
                        type: 'post',
                        url: '/Cate/advanced/edit',
                        data: data,
                        dataType: 'json',
                        success: function (res) {
                            layer.msg(res.msg, {time: 500, icon: 6})
                            //刷新树
                            tree.reload('ptreeID');
                        }
                    })

                } else if (type == 'del') {
                    //debugger
                    //ajax删除 --- 修改isdel = 1
                    console.log(data.id);
                    $.ajax({
                        type: 'post',
                        url: '/Cate/advanced/del',
                        data: {categrayId:data.id},
                        dataType: 'json',
                        success: function (res) {
                            layer.msg(res.msg, {time: 500, icon: 6})

                            ajaxtree();
                        }
                    })

                    //刷新树
                }
            }
        }),
            //监听提交按钮
            form.on('submit(addNodeform)',function (data){
                console.log(data.field)
                $.ajax({
                    type:'post',
                    url:'/Cate/advanced/addTree',
                    data:data.field,
                    dataType:'json',
                    success:function (res){
                        //关闭所有弹框
                        layer.closeAll();
                        //刷新树
                        ajaxtree();
                    }
                })

                return false;
            })

    })
}

//    表的渲染
function ajaxtable(){
    layui.use(['table','form','tree'],function (){
        var table = layui.table,
            tree  = layui.tree,
            form = layui.form;

// 分页参数 1.页码 2.每页展示条数

        tableIn = table.render({
            elem:'#ptable',
            url:'/JsProduct/advanced/getAll',//数据接口
            height: 800,
            page:true,
            cols:[
                [
                    {field:"productId",title:'Id',hide:true},
                    {field:"productCode",title:'商品编号',width: 120,fixed:'left'},
                    {field: 'mainImage', title: '商品图片',width: 100,templet:function(res){
                            return '<div><img src="'+res.mainImage+'" width="70px" height="70px"></a></div>';
                        }},
                    {field:"productName",title: "商品名称",width: 120},
                    {field:"currentpriceProduct",title: "商品价格",width: 120},
                    {field: "state", title: '上架/下架',width: 120,templet:function (res){
                        if (res.state==0){
                            return'<input type="checkbox" checked="checked" name="state" lay-filter="switchTest" lay-skin="switch" lay-text="上架|下架" productId=' + res.productId + '>'
                        }else if (res.state==1){
                            return '<input type="checkbox" name="state" lay-filter="switchTest" lay-skin="switch" lay-text="上架|下架" productId=' + res.productId + '>'
                        }
                        }},
                    {field:"categroyId",title: "商品分类",width: 120},
                    {field:"productLabel",title: "商品标签",width: 120},
                    {field: 'productSate',width: 100,height: 70, title: '审核状态',
                        templet: function (d) {return aa(d.productSate,d.productId)}},
                    {title:"操作",minWidth:200,toolbar:'#tablebar',align:"center"}

                ]
            ],

            parseData:function (res){//res 即为原始返回的数据
                return{
                    "code":res.state,//解析接口状态
                    "msg" :res.message,//解析提示文本
                    "count":res.total,//解析数据长度
                    "data":res.data//解析数据列表
                };
            },
            toolbar:'#toolbarbtn'
        });


        //头工具栏监听事件
        table.on('toolbar(ptablefilter)',function (obj){
            var type = obj.event;
            if (type == 'add'){
                        layer.open({
                            type:2,
                            title:'添加',
                            content:"../../pages/product/productadd.html",
                            area:['65%','100%'],
                            success:function (layero,index){
                                //成功打开iframe后的回调函数
                                var body = layer.getChildFrame('body',index);
                            }
                        })

            }
        })


        table.on('tool(ptablefilter)',function (obj){
            var type = obj.event;

            if (type == 'shanchu') {
                $.ajax({
                    type:'post',
                    url: '/JsProduct/advanced/addrizhi',
                    data:{operatelist:'商品列表',operatetype:'删除',operateremark:'删除商品信息'},
                    success:function(res){
                        if(top.window['refresh']){
                            top.window['refresh'].frames.location.reload();
                        }
                    }
                })
                layer.confirm("您确定要删除吗？",function (index){
                    //删除
                    //ajax请求
                    $.ajax({
                        type: 'post',
                        url: '/JsProduct/advanced/delete',
                        data: {"productId":obj.data.productId},
                        dataType: 'json',
                        success: function (res) {
                            layer.msg(res.msg, {time: 500, icon: 6})
                            //刷表
                            tableshua();
                        }
                    })

                    layer.close(index);

                })

            }else if(type == 'bianji'){
                layer.open({
                    type:2,
                    title:'编辑商品',
                    content:"../../pages/product/productupdate.html",
                    area:['65%','100%'],
                    success:function (layero,index){
                        //表单数据回显，把表格当前行显示到子页面的表单内，find后括号内的是子页面表单的id
                        var body = layer.getChildFrame('body',index);
                        body.find("#productCode").val( obj.data.productCode);
                        body.find("#demo1").attr("src",obj.data.mainImage);
                        body.find("#productName").val(obj.data.productName);
                        body.find("#currentpriceProduct").val(obj.data.currentpriceProduct);
                        body.find("#categroyId").val(obj.data.categroyId);
                        body.find("#productLabel").val(obj.data.productLabel);
                        body.find("#productSate").val(obj.data.productSate);
                        body.find("#productId").val(obj.data.productId);

                        tableshua();
                    }
                });
            }else if(type == 'chakan'){
                $.ajax({
                    type:'post',
                    url: '/JsProduct/advanced/addrizhi',
                    data:{operatelist:'商品列表',operatetype:'查看',operateremark:'查看商品详情'},
                    success:function(res){
                        if(top.window['refresh']){
                            top.window['refresh'].frames.location.reload();
                        }
                    }
                })
                layer.open({
                    type:2,
                    title:'查看商品',
                    content:"../../pages/product/productchakan.html",
                    area:['65%','100%'],
                    success:function (layero,index){
                        //表单数据回显，把表格当前行显示到子页面的表单内，find后括号内的是子页面表单的id
                        var body = layer.getChildFrame('body',index);
                        body.find("#productCode").val( obj.data.productCode);
                        body.find("#ylt").attr("src",obj.data.mainImage);
                        body.find("#productName").val(obj.data.productName);
                        body.find("#currentpriceProduct").val(obj.data.currentpriceProduct);
                        body.find("#categroyId").val(obj.data.categroyId);
                        body.find("#productLabel").val(obj.data.productLabel);
                        body.find("#productSate").val(obj.data.productSate);
                        body.find("#productId").val(obj.data.productId);
                        body.find("#createDate").val( obj.data.createDate);
                        body.find("#createUser").val(obj.data.createUser);
                        body.find("#searchKeywords").val(obj.data.searchKeywords);
                        body.find("#categroyId1").val(obj.data.categroyId);
                        body.find("#state").val(obj.data.state);
                        body.find("#xiaoliang").val(obj.data.xiaoliang);
                        body.find("#ensure").val(obj.data.ensure);
                        body.find("#fabulous").val(obj.data.fabulous);
                        body.find("#collectionVolume").val(obj.data.collectionVolume);
                        body.find("#hits").val(obj.data.hits);

                    }
                });
            }else if(type == 'rizhi'){
                layer.open({
                    type:2,
                    title:'查看日志',
                    content:"../../pages/product/productrizhi.html",
                    area:['65%','100%'],
                    success:function (layero,index){


                    }
                });
            }


        });







        //开关点击事件
        form.on('switch(switchTest)', function (data) {
            var state = this.checked ? "1" : "2"; //锁定状态赋值为1，未锁定赋值为2，根据数据库中的值
            console.log("111111111")
            $.ajax({
                url: '/JsProduct/advanced/state',
                type: 'post',
                dataType: 'json',
                data: {
                    'productId': data.elem.attributes['productId'].nodeValue,
                    'state': state
                },
                success: function (res) {
                    console.log(res);
                },
                error:function (res){
                    console.log("shibai")
                },
            });

        });








    })

}
